package br.mia.unifor.onaga.cloud.execute;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import br.mia.unifor.onaga.cloud.artifact.Instance;
import br.mia.unifor.onaga.cloud.artifact.Provider;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.ModifyInstanceAttributeRequest;

public class EC2ComputeProvider extends ComputeProvider {
	protected static AmazonEC2 ec2;

	public EC2ComputeProvider(Provider provider) throws IOException {
		super(provider);

		AWSCredentials credentials;
		
		InputStream input = Main.class.getClass()
				.getResourceAsStream(provider.getCredentialPath());
		if(input == null){			
			input = new FileInputStream(provider.getCredentialPath());
		}

		credentials = new PropertiesCredentials(input);
		ec2 = new AmazonEC2Client(credentials);
		input.close();

	}

	public boolean changeInstanceType(Instance instance) {
		
		String id = instance.getId().split("/")[1];
		
		ModifyInstanceAttributeRequest request = new ModifyInstanceAttributeRequest(
				id, "instanceType");
		request.setValue(instance.getType().getProviderProfile());

		ec2.modifyInstanceAttribute(request);

		return true;
	}
}
